#include<bits/stdc++.h>
typedef struct Node{
	int coef;
	int exp;
	struct Node *next;
}NODE,*LinkList;
LinkList create(LinkList L){
	 L=(LinkList)malloc(sizeof(NODE));
	LinkList p,q;
	p=L;
	int COEF,EXP,n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d %d",&COEF,&EXP);
		if(COEF!=0)
		q=(LinkList)malloc(sizeof(NODE));
		q->coef=COEF;
		q->exp=EXP;
		p->next=q;
		q->next=NULL;
		p=q;
	}
	return L;
}
LinkList print(LinkList L){
	LinkList q;
	q=L->next;
	if(q!=NULL){
				while(q!=NULL){
			printf(" %d %d ",q->coef,q->exp);
			q=q->next;
		}
	}
	else{
		printf("0 0");
	}
	return L;
}
LinkList sum(LinkList L1,LinkList L2){
	LinkList L=(LinkList)malloc(sizeof(NODE));
	LinkList p1,p2,p;
	p=L;
	p1=L1->next;
	p2=L2->next;
	while(p1!=NULL&&p2!=NULL){
		if(p1->exp>p2->exp){
			p->next=p1;
			p=p1;
			p1=p1->next;
		}
		else if(p1->exp<p2->exp){
			p->next=p2;
			p=p2;
			p2=p2->next;
		}
		else{
			if(p1->coef+p2->coef!=0){
				LinkList q=(LinkList)malloc(sizeof(NODE)); 
				q->coef=p1->coef+p2->coef;
				q->exp=p1->exp;
				p->next=q;
				p=q; 
			}
			p1=p1->next;
			p2=p2->next;
		}
	}
	if(p1){
		p->next=p1;
	}
	else if(p2){
		p->next=p2;
	}
	else{
		p->next=NULL;
	}
	return L; 
}
LinkList multify(LinkList L1,LinkList L2){
	LinkList L,temp=NULL,s=NULL;
	L=(LinkList)malloc(sizeof(NODE));
	temp=L;
	LinkList La=L1;
	while(La->next){
		s=(LinkList)malloc(sizeof(NODE));
		s->coef=La->next->coef*L2->next->coef;
		s->exp=La->next->exp+L2->next->exp;
		L->next=s;
		L=s;
		La=La->next;
	} 
	L->next=NULL;
	L=temp;
	LinkList Lb=L2->next;
	while(Lb&&Lb->next){
		 La=L1;
		while(La->next){
			s=(LinkList)malloc(sizeof(NODE));
			s->coef=La->next->coef*Lb->next->coef;
			s->exp=La->next->exp+Lb->next->exp;
			while(L->next){ 
			if(s->exp>L->next->exp){
				LinkList val=L->next;
				L->next=s;
				s->next=val;
				break;
			} 
			else if(s->exp==L->next->exp){
				L->next->coef+=s->coef;
				break;
			}
			L=L->next;
		}
		L->next=s;
		s->next=NULL;
		L=temp;
		La=La->next;
	}
	Lb=Lb->next;
   }
   return temp;
}
int main(){
	LinkList L1,L2,L;
	L1=create(L1);
	L2=create(L2);
	//L=sum(L1,L2);
	//print(L);
	L=multify(L1,L2);
	print(L);
	return 0;
}
